package slowka;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;

/**
 *
 * @author ssobczak
 */
public class TestPanel extends javax.swing.JPanel {
    private ResourceBundle resourceBundle;
    /** Creates new form TestPanel
     * @param s suites that can be choosen for tests
     */
    public TestPanel(List<Suite> s, ActionListener al, ResourceBundle res) {
        suites = s;
        callback = al;
        resourceBundle = res;
        initComponents();
        listSuites();
    }

    public void setSuites(List<Suite> s)
    {
        suites = s;
    }

    /** Get list of names of suites */
    private Vector<String> getSuitesNames() {
        Vector<String> names = new Vector<String>();
        for(Suite s : suites)
            names.add(resourceBundle.getString(s.getName()));
        return names;
    }

    /** Lists suites names in ComboBox */
    private void listSuites() {
        DefaultComboBoxModel M = new DefaultComboBoxModel(getSuitesNames());

        SuitesList.setModel(M);
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        jScrollPane3 = new javax.swing.JScrollPane();
        SuitesList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        StartTestButton = new javax.swing.JButton();
        QuestionLanguage = new javax.swing.JComboBox();

        setPreferredSize(new java.awt.Dimension(299, 365));

        jScrollPane3.setViewportView(SuitesList);

        jLabel1.setText(resourceBundle.getString("ASK_QUESTIONS_ABOUT"));

        StartTestButton.setText(resourceBundle.getString("START_TEST!")); // NOI18N
        StartTestButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartTestButtonActionPerformed(evt);
            }
        });

        QuestionLanguage.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            resourceBundle.getString("ASK_IN_POLISH"),
            resourceBundle.getString("ASK_IN_ENGLISH"),
            resourceBundle.getString("MIX_LANGUAGES") }));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(QuestionLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8))
            .addGroup(layout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addComponent(StartTestButton))))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(QuestionLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
            .addComponent(StartTestButton)
            .addGap(81, 81, 81))
    );
    }//GEN-END:initComponents

    private void StartTestButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartTestButtonActionPerformed
        int selectedLanguage = QuestionLanguage.getSelectedIndex();
        JFrame testFrame = new Test(getSuitesToTest(), QuestionAskingMode.values()[selectedLanguage], callback, resourceBundle);
        testFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        testFrame.setVisible(true);

        callback.actionPerformed(new ActionEvent(this, 0, "disappear"));
    }//GEN-LAST:event_StartTestButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox QuestionLanguage;
    private javax.swing.JButton StartTestButton;
    private javax.swing.JList SuitesList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables

    private List<Suite> suites = new LinkedList<Suite>();
    private ActionListener callback;

    private List<Suite> getSuitesToTest() {
       callback.actionPerformed(new ActionEvent(this, 2, "reload suites"));
       List<Suite> toTest = new LinkedList<Suite>();

        for(int i : SuitesList.getSelectedIndices())
            for(Suite s : suites)
                if(resourceBundle.getString(s.getName()).contentEquals(SuitesList.getModel().getElementAt(i).toString()))
                    toTest.add(s);

        return toTest;
    }
}
